/***
 * Andmin - Joomla administrator access from Android phone
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
 * @author codegravity.com
 * @copyright (C) 2011 by Matej Koval - All rights reserved!
 * @website http://www.codegravity.com
 */

package com.codegravity.mobile.andmin;

import com.codegravity.mobile.andmin.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


/***
 * Activity for user to setting for the app
 */
public class AndminSettingActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
	public static boolean isSaveData = false;
	private EditText txtUrl;
	private EditText txtAdminFolder;
	private EditText txtJoomplaComponent;
	private EditText txtUsername;
	private EditText txtPassword;
	private EditText txtAuthUser;
	private EditText txtAuthPass;
	private Button btnSave;
	private Button btnCancel;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.setting);        
        init();        
    }
    /***
     * init control and data for screen.
     */
    private void init() {
    	// get control form layout
    	txtUrl = (EditText)findViewById(R.id.txtUrl);
    	txtAdminFolder = (EditText)findViewById(R.id.txtAdminFolder);
    	txtJoomplaComponent = (EditText) findViewById(R.id.txtJoomlaComponent);
    	txtUsername = (EditText)findViewById(R.id.txtUsername);
    	txtPassword = (EditText)findViewById(R.id.txtPassword);
    	txtAuthUser = (EditText)findViewById(R.id.txtHttpUser);
    	txtAuthPass = (EditText)findViewById(R.id.txtHTTPPass);
    	btnSave = (Button) findViewById(R.id.btnSave);
    	btnCancel = (Button) findViewById(R.id.btnCancel);    	
    	// display data from file configuration.
    	if (!AndminSetting.isFirstRun()) {
	    	txtUrl.setText(AndminSetting.getWebUrl());
	    	txtAdminFolder.setText(AndminSetting.getFolderAdministrator());
	    	txtJoomplaComponent.setText(AndminSetting.getJoomplaComponent());
	    	txtUsername.setText(AndminSetting.getUsername());
	    	txtPassword.setText(AndminSetting.getPassword());
	    	txtAuthUser.setText(AndminSetting.getHttpAuthUsername());
	    	txtAuthPass.setText(AndminSetting.getHttpAuthPassword());
    	}
    	// set event for buttons
    	btnSave.setOnClickListener(this);
    	btnCancel.setOnClickListener(this);
    }
    /***
     * Events when user clicks buttons
     */
	@Override
	public void onClick(View button) {
		// TODO Auto-generated method stub
		if (button == btnSave) {
			SaveSetting();
		}
		if (button == btnCancel) {
			isSaveData = false;
			this.finish();
		}
	}
	/***
	 * Save setting and finish the activity
	 */
	private void SaveSetting() {
		// TODO Auto-generated method stub
		if (checkInput()) {
			AndminSetting.setWebUrl(txtUrl.getText().toString());
			AndminSetting.setFolderAdministrator(txtAdminFolder.getText().toString());
			AndminSetting.setJoomplaComponent(txtJoomplaComponent.getText().toString());
			AndminSetting.setUsername(txtUsername.getText().toString());
			AndminSetting.setPassword(txtPassword.getText().toString());
			AndminSetting.setHttpAuthUsername(txtAuthUser.getText().toString());
			AndminSetting.setHttpAuthPassword(txtAuthPass.getText().toString());
			AndminSetting.Save(this);
			isSaveData = true;
			this.finish();
		}
	}
	/***
	 * check input param.
	 * @return
	 */
	private boolean checkInput() {
		// TODO Auto-generated method stub
		if (txtUrl.getText().toString().length() == 0) {
			Toast.makeText(this, "Please, input Your Web Url.", Toast.LENGTH_LONG).show();
			return false;
		}
		if (txtAdminFolder.getText().toString().length() == 0) {
			Toast.makeText(this, "Please, input Admin Folder.", Toast.LENGTH_LONG).show();
			return false;
		}
		if (txtJoomplaComponent.getText().toString().length() == 0) {
			Toast.makeText(this, "Please, input Joompla Component.", Toast.LENGTH_LONG).show();
			return false;
		}
		if (txtUsername.getText().toString().length() == 0) {
			Toast.makeText(this, "Please, input Username.", Toast.LENGTH_LONG).show();
			return false;
		}
		if (txtPassword.getText().toString().length() == 0) {
			Toast.makeText(this, "Please, input Password.", Toast.LENGTH_LONG).show();
			return false;
		}
		return true;
	}
}